home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / lxml / sax.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  7KB  |  261 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from xml.sax.handler import ContentHandler
  5. from lxml import etree
  6. from lxml.etree import ElementTree, SubElement
  7. from lxml.etree import Comment, ProcessingInstruction
  8.  
  9. class SaxError(etree.LxmlError):
  10.     pass
  11.  
  12.  
  13. def _getNsTag(tag):
  14.     if tag[0] == '{':
  15.         return tuple(tag[1:].split('}', 1))
  16.     else:
  17.         return (None, tag)
  18.  
  19.  
  20. class ElementTreeContentHandler(ContentHandler):
  21.     
  22.     def __init__(self, makeelement = None):
  23.         self._root = None
  24.         self._root_siblings = []
  25.         self._element_stack = []
  26.         self._default_ns = None
  27.         self._ns_mapping = {
  28.             None: [
  29.                 None] }
  30.         self._new_mappings = { }
  31.         if makeelement is None:
  32.             makeelement = etree.Element
  33.         
  34.         self._makeelement = makeelement
  35.  
  36.     
  37.     def _get_etree(self):
  38.         return ElementTree(self._root)
  39.  
  40.     etree = property(_get_etree, doc = _get_etree.__doc__)
  41.     
  42.     def setDocumentLocator(self, locator):
  43.         pass
  44.  
  45.     
  46.     def startDocument(self):
  47.         pass
  48.  
  49.     
  50.     def endDocument(self):
  51.         pass
  52.  
  53.     
  54.     def startPrefixMapping(self, prefix, uri):
  55.         self._new_mappings[prefix] = uri
  56.         
  57.         try:
  58.             self._ns_mapping[prefix].append(uri)
  59.         except KeyError:
  60.             self._ns_mapping[prefix] = [
  61.                 uri]
  62.  
  63.         if prefix is None:
  64.             self._default_ns = uri
  65.         
  66.  
  67.     
  68.     def endPrefixMapping(self, prefix):
  69.         ns_uri_list = self._ns_mapping[prefix]
  70.         ns_uri_list.pop()
  71.         if prefix is None:
  72.             self._default_ns = ns_uri_list[-1]
  73.         
  74.  
  75.     
  76.     def startElementNS(self, ns_name, qname, attributes = None):
  77.         (ns_uri, local_name) = ns_name
  78.         if ns_uri:
  79.             el_name = '{%s}%s' % ns_name
  80.         elif self._default_ns:
  81.             el_name = '{%s}%s' % (self._default_ns, local_name)
  82.         else:
  83.             el_name = local_name
  84.         if attributes:
  85.             attrs = { }
  86.             
  87.             try:
  88.                 iter_attributes = attributes.iteritems()
  89.             except AttributeError:
  90.                 iter_attributes = attributes.items()
  91.  
  92.             for name_tuple, value in iter_attributes:
  93.                 if name_tuple[0]:
  94.                     attr_name = '{%s}%s' % name_tuple
  95.                 else:
  96.                     attr_name = name_tuple[1]
  97.                 attrs[attr_name] = value
  98.             
  99.         else:
  100.             attrs = None
  101.         element_stack = self._element_stack
  102.         if self._root is None:
  103.             element = self._root = self._makeelement(el_name, attrs, self._new_mappings)
  104.             if self._root_siblings and hasattr(element, 'addprevious'):
  105.                 for sibling in self._root_siblings:
  106.                     element.addprevious(sibling)
  107.                 
  108.             
  109.             del self._root_siblings[:]
  110.         else:
  111.             element = SubElement(element_stack[-1], el_name, attrs, self._new_mappings)
  112.         element_stack.append(element)
  113.         self._new_mappings.clear()
  114.  
  115.     
  116.     def processingInstruction(self, target, data):
  117.         pi = ProcessingInstruction(target, data)
  118.         if self._root is None:
  119.             self._root_siblings.append(pi)
  120.         else:
  121.             self._element_stack[-1].append(pi)
  122.  
  123.     
  124.     def endElementNS(self, ns_name, qname):
  125.         element = self._element_stack.pop()
  126.         if ns_name != _getNsTag(element.tag):
  127.             raise SaxError('Unexpected element closed: {%s}%s' % ns_name)
  128.         
  129.  
  130.     
  131.     def startElement(self, name, attributes = None):
  132.         self.startElementNS((None, name), name, attributes)
  133.  
  134.     
  135.     def endElement(self, name):
  136.         self.endElementNS((None, name), name)
  137.  
  138.     
  139.     def characters(self, data):
  140.         last_element = self._element_stack[-1]
  141.         
  142.         try:
  143.             last_element = last_element[-1]
  144.             if not last_element.tail:
  145.                 pass
  146.             last_element.tail = '' + data
  147.         except IndexError:
  148.             if not last_element.text:
  149.                 pass
  150.             last_element.text = '' + data
  151.  
  152.  
  153.     ignorableWhitespace = characters
  154.  
  155.  
  156. class ElementTreeProducer(object):
  157.     
  158.     def __init__(self, element_or_tree, content_handler):
  159.         
  160.         try:
  161.             element = element_or_tree.getroot()
  162.         except AttributeError:
  163.             element = element_or_tree
  164.  
  165.         self._element = element
  166.         self._content_handler = content_handler
  167.         attr_class = AttributesNSImpl
  168.         import xml.sax.xmlreader
  169.         self._attr_class = attr_class
  170.         self._empty_attributes = attr_class({ }, { })
  171.  
  172.     
  173.     def saxify(self):
  174.         self._content_handler.startDocument()
  175.         element = self._element
  176.         if hasattr(element, 'getprevious'):
  177.             siblings = []
  178.             sibling = element.getprevious()
  179.             while getattr(sibling, 'tag', None) is ProcessingInstruction:
  180.                 siblings.append(sibling)
  181.                 sibling = sibling.getprevious()
  182.             for sibling in siblings[::-1]:
  183.                 self._recursive_saxify(sibling, { })
  184.             
  185.         
  186.         self._recursive_saxify(element, { })
  187.         if hasattr(element, 'getnext'):
  188.             sibling = element.getnext()
  189.             while getattr(sibling, 'tag', None) is ProcessingInstruction:
  190.                 self._recursive_saxify(sibling, { })
  191.                 sibling = sibling.getnext()
  192.         
  193.         self._content_handler.endDocument()
  194.  
  195.     
  196.     def _recursive_saxify(self, element, prefixes):
  197.         content_handler = self._content_handler
  198.         tag = element.tag
  199.         if tag is Comment or tag is ProcessingInstruction:
  200.             if tag is ProcessingInstruction:
  201.                 content_handler.processingInstruction(element.target, element.text)
  202.             
  203.             if element.tail:
  204.                 content_handler.characters(element.tail)
  205.             
  206.             return None
  207.         
  208.         new_prefixes = []
  209.         build_qname = self._build_qname
  210.         attribs = element.items()
  211.         if attribs:
  212.             attr_values = { }
  213.             attr_qnames = { }
  214.             for attr_ns_name, value in attribs:
  215.                 attr_ns_tuple = _getNsTag(attr_ns_name)
  216.                 attr_values[attr_ns_tuple] = value
  217.                 attr_qnames[attr_ns_tuple] = build_qname(attr_ns_tuple[0], attr_ns_tuple[1], prefixes, new_prefixes)
  218.             
  219.             sax_attributes = self._attr_class(attr_values, attr_qnames)
  220.         else:
  221.             sax_attributes = self._empty_attributes
  222.         (ns_uri, local_name) = _getNsTag(tag)
  223.         qname = build_qname(ns_uri, local_name, prefixes, new_prefixes)
  224.         for prefix, uri in new_prefixes:
  225.             content_handler.startPrefixMapping(prefix, uri)
  226.         
  227.         content_handler.startElementNS((ns_uri, local_name), qname, sax_attributes)
  228.         if element.text:
  229.             content_handler.characters(element.text)
  230.         
  231.         for child in element:
  232.             self._recursive_saxify(child, prefixes)
  233.         
  234.         content_handler.endElementNS((ns_uri, local_name), qname)
  235.         for prefix, uri in new_prefixes:
  236.             content_handler.endPrefixMapping(prefix)
  237.         
  238.         if element.tail:
  239.             content_handler.characters(element.tail)
  240.         
  241.  
  242.     
  243.     def _build_qname(self, ns_uri, local_name, prefixes, new_prefixes):
  244.         if ns_uri is None:
  245.             return local_name
  246.         
  247.         
  248.         try:
  249.             prefix = prefixes[ns_uri]
  250.         except KeyError:
  251.             prefix = prefixes[ns_uri] = 'ns%02d' % len(prefixes)
  252.             new_prefixes.append((prefix, ns_uri))
  253.  
  254.         return prefix + ':' + local_name
  255.  
  256.  
  257.  
  258. def saxify(element_or_tree, content_handler):
  259.     return ElementTreeProducer(element_or_tree, content_handler).saxify()
  260.  
  261.